<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/9 0009
 * Time: 16:19
 */

namespace app\currency\service;

use app\wechat\service\WechatService;
use think\Exception;

class WechatTemplateService
{
    static $template_list = [
        'receipt' => 'fravUjguK8sMD064DTNNVPysbzRr43ovhyXkt6Ch6sk',
        'create' => '5VaTTJceYMj2VJM_n9cflEMP4wA1Ey01u6II7N5gIaI',
        'service_start' => 'PKNwpQV422x3N69NHGCjNSg-qKd51M7Yvfna0NqgLFY',
        'service_end' => '7cXImHocR_uvnbDXBxncpZSFH0UbV13SJM3E2PlwiYY',
        'cleaner_received' => 'RbUenVzN97SxxechWVYu_cbxVkxfQddQ9HwQ250UEQU',
        'cleaner_order_payed' => 'L2zGNGEagY7I5NGR4FslqGk9v8qMoUwucPfSaSGUPDo'
    ];
    static $base_url = 'http://bjtest.j.xueao400.com/';

    /**
     * 订单创建提醒
     * @param string $openid
     * @param string $order_no
     * @param string $contact_name
     * @param string $contact_phone
     * @return array|string
     */
    public static function sendOrderCreate($openid, $order_no, $contact_name, $contact_phone){
        self::logs(json_encode([
            'openid' => $openid,
            'order_no' => $order_no,
            'contact_name' => $contact_name,
            'contact_phone' => $contact_phone
        ]));
        try{
            $customer_service_telephone_numbers = sysconf('customer_service_telephone_numbers');
            $param = [
                'touser' => $openid,
                'template_id' => self::$template_list['create'],
                'url' => self::$base_url . 'pages/user/order/orderlist/orderlist?ind=0',
                'topcolor' => '#FF0000',
                'data' => [
                    'first' => ['value' => '您好，您的订单创建成功'],
                    'keyword1' => ['value' => $order_no],
                    'keyword2' => ['value' => '房屋清洁'],
                    'keyword3' => ['value' => $contact_name],
                    'keyword4' => ['value' => $contact_phone],
                    'remark' => ['value' => "如有任何疑问，请联系客服：{$customer_service_telephone_numbers}"],
                ]
            ];
            return WechatService::WeChatTemplate()->send($param);
        }catch (\Exception $e){
            self::logs($e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 订单开始提醒
     */
    public static function sendOrderServiceStart($openid, $order_no, $dateTime){
        self::logs(json_encode([
            'openid' => $openid,
            'order_no' => $order_no,
            'dateTime' => $dateTime
        ]));
        try{
            $param = [
                'touser' => $openid,
                'template_id' => self::$template_list['service_start'],
                'url' => self::$base_url . 'pages/user/order/orderlist/orderlist?ind=1',
                'topcolor' => '#FF0000',
                'data' => [
                    'first' => ['value' => '您好，您的清洁服务已开始！'],
                    'keyword1' => ['value' => $order_no],
                    'keyword2' => ['value' => $dateTime],
                    'remark' => ['value' => "感谢您的支持！"],
                ]
            ];
            return WechatService::WeChatTemplate()->send($param);
        }catch (\Exception $e){
            self::logs($e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 订单结束提醒
     */
    public static function sendOrderServiceEnd($openid, $order_no, $start_time, $end_time){
        self::logs(json_encode([
            'openid' => $openid,
            'order_no' => $order_no,
            'start_time' => $start_time,
            'end_time' => $end_time
        ]));
        try{
            $param = [
                'touser' => $openid,
                'template_id' => self::$template_list['service_end'],
                'url' => self::$base_url . 'pages/user/order/orderlist/orderlist?ind=2',
                'topcolor' => '#FF0000',
                'data' => [
                    'first' => ['value' => '您好，您的清洁服务已结束！'],
                    'keyword1' => ['value' => $order_no],
                    'keyword2' => ['value' => $start_time],
                    'keyword3' => ['value' => $end_time],
                    'keyword4' => ['value' => '正常'],
                    'remark' => ['value' => "感谢使用，欢迎再次使用！"],
                ]
            ];
            return WechatService::WeChatTemplate()->send($param);
        }catch (\Exception $e){
            self::logs($e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 新订单提醒
     */
    public static function sendNewOrder(){}

    /**
     * 待接单提醒
     * @param string $openid openid
     * @param string $order_no 订单号
     * @param string $cleaner_name 服务者
     * @param string $cleaner_phone 联系电话
     * @param string $create_at 创建时间
     * @return array|string
     */
    public static function sendCleanerReceived($openid = '', $order_no = '', $cleaner_name = '', $cleaner_phone = '', $create_at = ''){
        self::logs(json_encode([
            'openid' => $openid,
            'cleaner_name' => $cleaner_name,
            'cleaner_phone' => $cleaner_phone,
        ]));
        try{
            $param = [
                'touser' => $openid,
                'template_id' => self::$template_list['cleaner_received'],
                'url' => self::$base_url . 'pages/serve/order/orderlist/orderlist',
                'topcolor' => '#FF0000',
                'data' => [
                    'first' => ['value' => '你有一个新的待接订单'],
                    'keyword1' => ['value' => $order_no],
                    'keyword2' => ['value' => $cleaner_name],
                    'keyword3' => ['value' => $cleaner_phone],
                    'keyword4' => ['value' => $create_at],
                    'remark' => ['value' => '点击此处查看详细信息'],
                ]
            ];
            return WechatService::WeChatTemplate()->send($param);
        }catch (\Exception $e){
            self::logs($e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 订单返利提醒
     */
    public static function sendOrderRoyalty(){}

    /**
     * 订单接单提醒
     * @param string $openid
     * @param string $cleaner_name
     * @param string $cleaner_phone
     * @return array|string
     */
    public static function sendOrderReceive($openid = '', $cleaner_name = '', $cleaner_phone = ''){
        self::logs(json_encode([
            'openid' => $openid,
            'cleaner_name' => $cleaner_name,
            'cleaner_phone' => $cleaner_phone,
        ]));
        try{
            $param = [
                'touser' => $openid,
                'template_id' => self::$template_list['receipt'],
                'url' => self::$base_url . 'pages/user/order/orderlist/orderlist?ind=1',
                'topcolor' => '#FF0000',
                'data' => [
                    'first' => ['value' => '您好，您的订单已接单成功，稍后会有工作人员为您上门服务'],
                    'keyword1' => ['value' => '家政保洁'],
                    'keyword2' => ['value' => '房屋清洁'],
                    'keyword3' => ['value' => $cleaner_name],
                    'keyword4' => ['value' => $cleaner_phone],
                    'remark' => ['value' => '点击此处查看详细信息'],
                ]
            ];
            return WechatService::WeChatTemplate()->send($param);
        }catch (\Exception $e){
            self::logs($e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 订单支付成功提醒（给保洁阿姨的提醒）
     * @param string $openid
     * @param string $order_no
     * @param string $user_name
     * @param string $price
     * @param string $time
     * @return array|string
     */
    public static function sendCleanerOrderPayed($openid = '',$order_no = '', $user_name = '', $price = '', $time = '')
    {
        self::logs(json_encode([
            'openid' => $openid,
            'order_no' => $order_no,
            'user_name' => $user_name,
            'price' => $price,
            'time' => $time
        ]));
        try{
            $param = [
                'touser' => $openid,
                'template_id' => self::$template_list['cleaner_payed'],
                'url' => self::$base_url . 'pages/serve/order/orderlist/orderlist',
                'topcolor' => '#FF0000',
                'data' => [
                    'first' => ['value' => '有用户支付啦'],
                    'keyword1' => ['value' => $order_no],
                    'keyword2' => ['value' => '服务费'],
                    'keyword3' => ['value' => $user_name],
                    'keyword4' => ['value' => $price],
                    'keyword5' => ['value' => $time],
                    'remark' => ['value' => '点击此处查看详细信息'],
                ]
            ];
            return WechatService::WeChatTemplate()->send($param);
        }catch (\Exception $e){
            self::logs($e->getMessage());
            return $e->getMessage();
        }
    }
    protected static function logs($string)
    {
        file_put_contents('wechat_template.log',$string.PHP_EOL,FILE_APPEND);
    }
}